package com.tz.online.book.service.impl;

import java.util.List;

import com.tz.online.book.dao.IBookDao;
import com.tz.online.book.dao.ICategoryDao;
import com.tz.online.book.service.IBookService;
import com.tz.online.common.PageBean;
import com.tz.online.entity.Book;
import com.tz.online.entity.Category;
import com.tz.online.util.BeanFactory;

/**
 * 商品模块业务层实现
 * @author Administrator
 *
 */
public class BookServiceImpl implements IBookService {
	//通过bean工厂，获得数据访问层接口对象
	private IBookDao ibd = (IBookDao) BeanFactory.getBean("bookDao");
	private ICategoryDao cDao = (ICategoryDao) BeanFactory.getBean("categoryDao");

	@Override
	public PageBean<Book> queryByCondition(int now, int size, String cate) {
		//1.调用dao获取数据
		List<Book> books = ibd.selectByCondition(now, size, cate);
		//2.获取总记录数
		int total = ibd.rowCount(cate);
		//3.创建PageBean对象
		PageBean<Book> pb = new PageBean<>(total, size);
		//4.设置当前的页数和数据
		pb.setCurrent(now);
		pb.setDatas(books);
		//5.返回
		return pb;
	}

	@Override
	public Book queryById(Long id) {
		//直接调用dao
		return ibd.findById(id);
	}

	@Override
	public List<Category> getAllCategories() {
		return cDao.getAll();
	}
}
